package com.wtx.atguigu.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description:
 * @author: wtx
 * @createDate: 2020/6/8
 */
public class NioFileChannel01 {

    public static void main(String[] args) throws IOException {

        String str = "hello atguigu";
        FileOutputStream fileOutputStream = new FileOutputStream("file01.txt");

        /**
         * The associated channel, initialized lazily.
         * private FileChannel channel;
         */
        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());

        //记得flip, 因为Channel要从Buffer中读取数据
        byteBuffer.flip();

        // public abstract int write(ByteBuffer src): src -> sourc
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}
